/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.admin.controller;

import com.google.common.collect.Ordering;
import com.lvyh.lightframe.job.admin.annotation.PermitLimit;
import com.lvyh.lightframe.job.admin.bean.JobTaskHistoryBean;
import com.lvyh.lightframe.job.admin.util.DateUtils;
import com.lvyh.lightframe.job.admin.processor.JobManageProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author lvyh 2021/04/11.
 */
@Controller
@RequestMapping("/history")
public class HistoryController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(HistoryController.class.getName());

    @Autowired
    private JobManageProcessor jobManageProcessor;

    @RequestMapping
    @PermitLimit(superAdmin = true)
    public String index(Model model) throws Exception {

        List<JobTaskHistoryBean> dataList = jobManageProcessor.getJobTaskHistoryList();
        dataList.sort(Ordering.natural().onResultOf(JobTaskHistoryBean::getCreateTime));

        dataList.forEach(p -> p.setScheduleTimeText(DateUtils.format(p.getScheduleTime())));
        dataList.forEach(p -> p.setTaskStartDateText(DateUtils.format(p.getTaskStartDate())));
        dataList.forEach(p -> p.setTaskEndDateText(DateUtils.format(p.getTaskEndDate())));
        model.addAttribute("dataList", dataList);

        return "history/list";
    }


}
